<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests;

use View;
use App\Model\Hy;

class HyController extends AdminBaseController
{
    public function __construct()
    {
        $option = ['module_name'=>'hy','status'=>'status'];
        $this->setModelObj(new Hy,$option);
    }

    /**
     * 获取正确的排序组合
     */
    protected function getTrueOrder($data)
    {
        $orderby = [];
        if(!array_key_exists('sort',$data)){
            $orderby['field'] = $this->model_keyname;
            $orderby['type'] = 'desc';
        }else{
            switch ($data['sort']){
                case 'classify_format':
                    $orderby['field'] = 'classify';break;
                case 'status_format':
                    $orderby['field'] = 'status';break;
                case 'edit_time_format':
                    $orderby['field'] = 'edit_time';break;
                default:
                    $orderby['field'] = $data['sort'];break;
            }
            $orderby['type'] =  $data['order'];
        }
        return $orderby;
    }

	/**
	 * 后台会议添加保存
	 */
	public function toAddData(Requests\HyRequest $request)
	{
		$data = $request->except(['_token','_method']);
		
		//Base64 保存图片
		$fileroot = 'uploads/images/'.date("Ymd").'/';
		$data['title_pic'] = isset($data['title_pic']) ? $this->basePic($data['title_pic'],$fileroot):'';
		
		$data['status'] = 1;
		$data['add_time'] = time();
		$data['edit_time'] = time();
		$re = Hy::insert($data);
        $jsonData = $re ? $this->getJsonData(2,'增加成功') : $this->getJsonData(0,'增加失败');
		return response()->json($jsonData);
	}

	/**
	 * 后台会议编辑保存
	 */
	public function toUpdateData(Requests\HyRequest $request, $hyid)
	{
		$data = $request->except(['_token','_method']);
		
		//Base64 保存图片
		$fileroot = 'uploads/images/'.date("Ymd").'/';
        $data['title_pic'] = isset($data['title_pic']) ? $this->basePic($data['title_pic'],$fileroot):'';
		
		$data['edit_time'] = time();
		
		$re = Hy::where('id',$hyid)->update($data);
        $jsonData = $re ? $this->getJsonData(2,'编辑成功') : $this->getJsonData(0,'编辑失败');
		return response()->json($jsonData);
	}

}